package com.xxxy.ljh.myapplication.nav;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.fragment.app.Fragment;

import com.xxxy.ljh.myapplication.R;
import com.google.android.material.card.MaterialCardView;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public HomeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HomeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        // 初始化快捷功能卡片并设置点击事件
        initShortcutCards(view);

        return view;


    }

    private void initShortcutCards(View view) {
        // 社团中心卡片

        View clubCenterCard = view.findViewById(R.id.club_center_card);
        if (clubCenterCard == null) {
            // 如果在布局中没有ID，则通过位置查找
            ViewGroup gridLayout = view.findViewById(R.id.home_grid_layout);
            if (gridLayout != null && gridLayout.getChildCount() > 0) {
                View cardView = gridLayout.getChildAt(0);
                if (cardView instanceof ViewGroup) {
                    cardView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // 跳转到社团页面逻辑
                            navigateToClubPage();
                        }
                    });
                }
            }
        } else {
            clubCenterCard.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    navigateToClubPage();
                }
            });
        }

        // 活动中心卡片
        View activityCenterCard = view.findViewById(R.id.activity_center_card);
        if (activityCenterCard == null) {
            ViewGroup gridLayout = view.findViewById(R.id.home_grid_layout);
            if (gridLayout != null && gridLayout.getChildCount() > 1) {
                View cardView = gridLayout.getChildAt(1);
                if (cardView instanceof ViewGroup) {
                    cardView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // 跳转到活动页面逻辑
                            navigateToActivityPage();
                        }
                    });
                }
            }
        } else {
            activityCenterCard.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    navigateToActivityPage();
                }
            });
        }

        // 个人中心卡片
        View profileCenterCard = view.findViewById(R.id.profile_center_card);
        if (profileCenterCard == null) {
            ViewGroup gridLayout = view.findViewById(R.id.home_grid_layout);
            if (gridLayout != null && gridLayout.getChildCount() > 2) {
                View cardView = gridLayout.getChildAt(2);
                if (cardView instanceof ViewGroup) {
                    cardView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // 跳转到个人中心页面逻辑
                            navigateToProfilePage();
                        }
                    });
                }
            }
        } else {
            profileCenterCard.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    navigateToProfilePage();
                }
            });
        }

        // 系统设置卡片
        View settingsCard = view.findViewById(R.id.settings_card);
        if (settingsCard == null) {
            ViewGroup gridLayout = view.findViewById(R.id.home_grid_layout);
            if (gridLayout != null && gridLayout.getChildCount() > 3) {
                View cardView = gridLayout.getChildAt(3);
                if (cardView instanceof ViewGroup) {
                    cardView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // 跳转到设置页面逻辑
                            navigateToSettingsPage();
                        }
                    });
                }
            }
        } else {
            settingsCard.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    navigateToSettingsPage();
                }
            });
        }
    }

    // 跳转到社团页面
    private void navigateToClubPage() {
        // 由于已经在主界面中，可以通过ViewPager切换到社团页面
        if (getActivity() instanceof com.xxxy.ljh.myapplication.MainActivity) {
            com.xxxy.ljh.myapplication.MainActivity mainActivity =
                    (com.xxxy.ljh.myapplication.MainActivity) getActivity();
            // 切换到第二个tab（社团页面，索引为1）
            mainActivity.getViewPager2().setCurrentItem(1, true);
        }
    }

    // 跳转到活动页面
    private void navigateToActivityPage() {
        if (getActivity() instanceof com.xxxy.ljh.myapplication.MainActivity) {
            com.xxxy.ljh.myapplication.MainActivity mainActivity =
                    (com.xxxy.ljh.myapplication.MainActivity) getActivity();
            // 切换到第三个tab（活动页面，索引为2）
            mainActivity.getViewPager2().setCurrentItem(2, true);
        }
    }

    // 跳转到个人中心页面
    private void navigateToProfilePage() {
        if (getActivity() instanceof com.xxxy.ljh.myapplication.MainActivity) {
            com.xxxy.ljh.myapplication.MainActivity mainActivity =
                    (com.xxxy.ljh.myapplication.MainActivity) getActivity();
            // 切换到第四个tab（个人中心页面，索引为3）
            mainActivity.getViewPager2().setCurrentItem(3, true);
        }
    }

// 跳转到设置页面
private void navigateToSettingsPage() {
    Intent intent = new Intent(getActivity(), com.xxxy.ljh.myapplication.activity.login.SettingsActivity.class);
    startActivity(intent);
}


}

